﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Text.RegularExpressions;

namespace _4DJackPot
{
    public partial class Join : Dock
    {

        Boolean userVerify;
        Boolean pwVerify;
        public Join()
        {
            InitializeComponent();
            userVerify = false;
            pwVerify = false;

        }

        private void bTn_Exit_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        
        private void bTn_SaveUser_Click(object sender, EventArgs e)
        {
            if (checkEmpty(1))//디비에 유저 추가
            {
                if (!userVerify)
                {
                    MessageBox.Show("The entered ID already exists. Please enter another ID", "Help", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
                if (!pwVerify)
                {
                    MessageBox.Show("The entered passwords are not same. Please check the password.", "Help", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
                
                //db에 새로운 사용자 정보 삽입
                string idString = this.tB_UserID.Text ?? "";
                string pwString = this.tB_Password.Text ?? "";
                string phoneString = this.tB_Phone1.Text ?? "";
                string addrString = this.tB_Address.Text ?? "";
                string nameString = this.tB_Email.Text ?? "";

                //insert query 작성
                string currentDate = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                string insertQueryString =
                    "INSERT INTO users(user_id,`password`,`name`,permission_id,registered_datetime) " +
                "VALUES('" + idString + "','" + pwString + "','" + nameString + "',1,'" + currentDate + "')";
//                MessageBox.Show(insertQueryString);
                DbConn dbconn = new DbConn();
                if (dbconn.InsertQuery(insertQueryString))
                {
                    MessageBox.Show("Registered. You can login and use.", "Help", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    this.Close();
                }
                                
            }
            else//필수입력사항이 입력되지 않은 경우
            {
                MessageBox.Show("Please fill in the required blank.", "Help", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void bTn_SaveCustomer_Click(object sender, EventArgs e)
        {
            if (checkEmpty(2))//디비에 유저 추가
            {
                if (!userVerify)
                {
                    MessageBox.Show("The entered ID already exists. Please enter another ID", "Help", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
                if (!pwVerify)
                {
                    MessageBox.Show("The entered passwords are not same. Please check the password.", "Help", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
                //db에 새로운 사용자 정보 삽입
                string idString = this.textBox6.Text ?? "";
                string pwString = this.textBox5.Text ?? "";
                string phoneString = this.textBox1.Text ?? "";
                string addrString = this.textBox3.Text ?? "";
                string nameString = this.textBox2.Text ?? "";

                //insert query 작성
                string currentDate = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");

                string insertQueryString =
                    "INSERT INTO users(user_id,`password`,`name`,permission_id,registered_datetime) " +
                "VALUES('" + idString + "','" + pwString + "','"+nameString+"',2,'" + currentDate + "')";
 //               MessageBox.Show(insertQueryString);
                        
                DbConn DbConn = new DbConn();
                if (DbConn.InsertQuery(insertQueryString))
                {
                    MessageBox.Show("Registered. You can login and use.", "Help", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    this.Close();
                }

            }
            else//필수입력사항이 입력되지 않은 경우
            {
                MessageBox.Show("Please fill in the required blank.", "Help", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private Boolean checkEmpty(int mode)
        {
            if ((tB_UserID.Text==""&&mode==1)||(textBox6.Text==""&&mode==2))
            {
                return false;
            }
            if ((tB_Password.Text == ""&&mode==1)||(textBox5.Text==""&&mode==2))
            {
                return false;
            }
            if ((tB_PasswordCheck.Text == ""&&mode==1)||(textBox4.Text==""&&mode==2))
            {
                return false;
            }
            if ((tB_Email.Text == "" && mode == 1) || (textBox2.Text == ""&&mode==2))
            {
                return false;
            }

            return true;
        }

        private void tB_Phone_TextChanged(object sender, EventArgs e)
        {
            Regex regex = new Regex(@"[^\d]+");
            if (!regex.IsMatch(tB_Phone1.Text))
            {
            }
            else
            {
                String str = tB_Phone1.Text;
                str = str.Substring(0, str.Length - 1);
                tB_Phone1.Text = str;
                tB_Phone1.Focus();
                tB_Phone1.Select(tB_Phone1.Text.Length, 0);
            }
            if (tB_Phone1.Text.Length == 3)
                tB_Phone2.Focus();
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            Regex regex = new Regex(@"[^\d]+");
            if (!regex.IsMatch(textBox1.Text))
            {
            }
            else
            {
                String str = textBox1.Text;
                str = str.Substring(0, str.Length - 1);
                textBox1.Text = str;
                textBox1.Focus();
                textBox1.Select(textBox1.Text.Length, 0);
            }
            if (textBox1.Text.Length == 3)
                textBox7.Focus();
        }

        private void tB_UserID_TextChanged(object sender, EventArgs e)
        {
            Regex re = new Regex(@"\w");
            Array al; 
            al = tB_UserID.Text.ToCharArray(); 
            bool result = true; 
            for (int i = 0; i < al.Length; i++) 
            { 
                result = re.IsMatch(al.GetValue(i).ToString()); 
                if (result == false) 
                {
                    int CursorPoint = tB_UserID.SelectionStart;
                    string Text = tB_UserID.Text.Remove(i, 1);
                    tB_UserID.Text = Text;
                    tB_UserID.SelectionStart = CursorPoint; 
                } 
            } 
        }
        
        private void tB_Phone2_TextChanged(object sender, EventArgs e)
        {
            Regex regex = new Regex(@"[^\d]+");
            if (!regex.IsMatch(tB_Phone2.Text))
            {
            }
            else
            {
                String str = tB_Phone2.Text;
                str = str.Substring(0, str.Length - 1);
                tB_Phone2.Text = str;
                tB_Phone2.Focus();
                tB_Phone2.Select(tB_Phone2.Text.Length, 0);
            }
            if (tB_Phone2.Text.Length == 4)
                tB_Phone3.Focus();
        }

        private void tB_Phone3_TextChanged(object sender, EventArgs e)
        {
            Regex regex = new Regex(@"[^\d]+");
            if (!regex.IsMatch(tB_Phone3.Text))
            {
            }
            else
            {
                String str = tB_Phone3.Text;
                str = str.Substring(0, str.Length - 1);
                tB_Phone3.Text = str;
                tB_Phone3.Focus();
                tB_Phone3.Select(tB_Phone3.Text.Length, 0);
            }
        }

        private void textBox7_TextChanged(object sender, EventArgs e)
        {
            Regex regex = new Regex(@"[^\d]+");
            if (!regex.IsMatch(textBox7.Text))
            {
            }
            else
            {
                String str = textBox7.Text;
                str = str.Substring(0, str.Length - 1);
                textBox7.Text = str;
                textBox7.Focus();
                textBox7.Select(textBox7.Text.Length, 0);
            }
            if (textBox7.Text.Length == 4)
                textBox8.Focus();
        }

        private void textBox8_TextChanged(object sender, EventArgs e)
        {
            Regex regex = new Regex(@"[^\d]+");
            if (!regex.IsMatch(textBox8.Text))
            {
            }
            else
            {
                String str = textBox8.Text;
                str = str.Substring(0, str.Length - 1);
                textBox8.Text = str;
                textBox8.Focus();
                textBox8.Select(textBox8.Text.Length, 0);
            }
        }

        private void tB_UserID_Leave(object sender, EventArgs e)
        {
            if (tB_UserID.Text == "")
            {
                tB_label.Text = "Please enter UserID";
            }
            else
            {                
                string idString = this.tB_UserID.Text ?? "";

                //select query 작성
                string selectQueryString =
                    "SELECT DISTINCT name, permission_id FROM users " +
                    "WHERE user_id = '" + idString + "'";

                DbConn DbConn = new DbConn();

                // DbConn에 ReadQuery 호출 하면 해당 쿼리가 실행되어 DataSet으로 Return됨
                DataSet resultReadDataSet = DbConn.ReadQuery(selectQueryString);

                if (resultReadDataSet == null)
                {
                    MessageBox.Show("Can`t connect to DB. Please check the DB.", "Help", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
                else if (resultReadDataSet.Tables[0].Rows.Count == 0)
                {
                    userVerify = true;
                    tB_label.Text = "Entered UserID is available";
                    return;
                }
                else
                {
                    userVerify = false;
                    tB_label.Text = "Entered UserID is not available";
                    tB_UserID.Focus();

                    return;
                }
            }
        }

        private void textBox6_Leave(object sender, EventArgs e)
        {
            if (textBox6.Text == "") 
            {
                tB_label2.Text = "Please enter UserID";
            }
            else
            {
                string idString = this.textBox6.Text ?? "";

                //select query 작성
                string selectQueryString =
                    "SELECT DISTINCT name, permission_id FROM users " +
                    "WHERE user_id = '" + idString + "'";

                DbConn DbConn = new DbConn();

                // DbConn에 ReadQuery 호출 하면 해당 쿼리가 실행되어 DataSet으로 Return됨
                DataSet resultReadDataSet = DbConn.ReadQuery(selectQueryString);

                if (resultReadDataSet == null)
                {
                    MessageBox.Show("Can`t connect to DB. Please check the DB.", "Help", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
                else if (resultReadDataSet.Tables[0].Rows.Count == 0)
                {
                    userVerify = true;
                    tB_label2.Text = "Entered UserID is available";
                    return;
                }
                else
                {
                    userVerify = false;
                    tB_label2.Text = "Entered UserID is not available";
                    return;
                }
            }
        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {
            Regex re = new Regex(@"\w");
            Regex regex = new Regex(@"[^\d]+");
            Array al;
            al = textBox2.Text.ToCharArray();
            bool result = true;
            for (int i = 0; i < al.Length; i++)
            {
                result = re.IsMatch(al.GetValue(i).ToString());
                if (result == false)
                {
                    int CursorPoint = textBox2.SelectionStart;
                    string Text = textBox2.Text.Remove(i, 1);
                    textBox2.Text = Text;
                    textBox2.SelectionStart = CursorPoint;
                }
                result = regex.IsMatch(al.GetValue(i).ToString());
                if (result == false)
                {
                    if (textBox2.Text == "")
                        return;
                    String str = textBox2.Text;
                    str = str.Substring(0, str.Length - 1);
                    textBox2.Text = str;
                    textBox2.Focus();
                    textBox2.Select(textBox2.Text.Length, 0);
                }
            }
        }

        private void tB_Email_TextChanged(object sender, EventArgs e)
        {
            Regex re = new Regex(@"\w");
            Regex regex = new Regex(@"[^\d]+");
            Array al;
            al = tB_Email.Text.ToCharArray();
            bool result = true;
            for (int i = 0; i < al.Length; i++)
            {
                result = re.IsMatch(al.GetValue(i).ToString());
                if (result == false)
                {
                    int CursorPoint = tB_Email.SelectionStart;
                    string Text = tB_Email.Text.Remove(i, 1);
                    tB_Email.Text = Text;
                    tB_Email.SelectionStart = CursorPoint;
                }
                result = regex.IsMatch(al.GetValue(i).ToString());
                if (result == false)
                {
                    if (tB_Email.Text == "")
                        return;
                    String str = tB_Email.Text;
                    str = str.Substring(0, str.Length - 1);
                    tB_Email.Text = str;
                    tB_Email.Focus();
                    tB_Email.Select(tB_Email.Text.Length, 0);
                }
            }

        }

        private void tB_PasswordCheck_Leave(object sender, EventArgs e)
        {
            if (!tB_Password.Text.Equals(tB_PasswordCheck.Text))
            {
                pwVerify = false;
                tB_plabel.Text = "Passwords are not same!";
            }
            else if (tB_Password.Text == "" || tB_PasswordCheck.Text == "")
            {
                tB_plabel2.Text = "Password blanks are empty!";
            }
            else
            {
                pwVerify = true;
                tB_plabel.Text = "Passwords are same!";
            }
        }

        private void textBox4_Leave(object sender, EventArgs e)
        {
            if (!textBox4.Text.Equals(textBox5.Text))
            {
                pwVerify = false;
                tB_plabel2.Text = "Passwords are not same!";
            }
            else if(textBox4.Text == "" || textBox5.Text == "")
            {
                tB_plabel2.Text = "Password blanks are empty!";
            }
            else
            {
                pwVerify = true;
                tB_plabel2.Text = "Passwords are same!";
            }
        }

        private void textBox6_TextChanged(object sender, EventArgs e)
        {
            Regex re = new Regex(@"\w");
            Array al;
            al = textBox6.Text.ToCharArray();
            bool result = true;
            for (int i = 0; i < al.Length; i++)
            {
                result = re.IsMatch(al.GetValue(i).ToString());
                if (result == false)
                {
                    int CursorPoint = textBox6.SelectionStart;
                    string Text = textBox6.Text.Remove(i, 1);
                    textBox6.Text = Text;
                    textBox6.SelectionStart = CursorPoint;
                }
            } 
        }

        private void textBox3_TextChanged(object sender, EventArgs e)
        {
            Regex re = new Regex(@"\w");
            Array al;
            al = textBox3.Text.ToCharArray();
            bool result = true;
            for (int i = 0; i < al.Length; i++)
            {
                result = re.IsMatch(al.GetValue(i).ToString());
                if (!result && al.GetValue(i).ToString() == " ")
                {
                    result = true;
                }
                if (result == false)
                {
                    int CursorPoint = textBox3.SelectionStart;
                    string Text = textBox3.Text.Remove(i, 1);
                    textBox3.Text = Text;
                    textBox3.SelectionStart = CursorPoint;
                }
            }
        }

        private void tB_Address_TextChanged(object sender, EventArgs e)
        {
            Regex re = new Regex(@"\w");
            Array al;
            al = tB_Address.Text.ToCharArray();
            bool result = true;
            for (int i = 0; i < al.Length; i++)
            {
                result = re.IsMatch(al.GetValue(i).ToString());
                if (!result && al.GetValue(i).ToString() == " ")
                {
                    result = true;
                }
                if (result == false)
                {
                    int CursorPoint = tB_Address.SelectionStart;
                    string Text = tB_Address.Text.Remove(i, 1);
                    tB_Address.Text = Text;
                    tB_Address.SelectionStart = CursorPoint;
                }
            }
        }

        private void label17_Click(object sender, EventArgs e)
        {

        }

    }
}